package com.lib.common.widget.recyclerView.drag

import androidx.annotation.NonNull
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

/**
 * Copyright (C), 2020-2020, 享物说
 * FileName: DragItemTouchHelperCallback
 * Author: keanbin
 * Date: 2020-02-11 22:57
 * Description: 拖拽 ItemTouchHelper 的 Callback
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
abstract class DragItemTouchHelperCallback: ItemTouchHelper.Callback() {

   override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
        //设置监听拖拽的方向
        val dragFlags =
            ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
        val swipeFlags = 0
        return makeMovementFlags(dragFlags, swipeFlags)
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}

    override fun isLongPressDragEnabled(): Boolean {
        //是否可拖拽
        return false
    }

    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        super.onSelectedChanged(viewHolder, actionState)

        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            viewHolder?.itemView?.scaleX = 1.1f
            viewHolder?.itemView?.scaleY = 1.1f
        }
    }

    override fun clearView(@NonNull recyclerView: RecyclerView, @NonNull viewHolder: RecyclerView.ViewHolder) {
        super.clearView(recyclerView, viewHolder)
        if (!recyclerView.isComputingLayout()) {
            //拖拽结束后恢复view的状态
            viewHolder.itemView.setScaleX(1.0f)
            viewHolder.itemView.setScaleY(1.0f)
        }
    }
}